<?php

namespace App\Http\Controllers;

use App\Http\Consts\LangConsts;
use App\Http\Consts\MapConsts;
use App\Http\Requests\ListRequest;
use App\Http\Requests\MapAddRequest;
use App\Models\Article;
use App\Models\Game;
use App\Models\Map;

class ManageTypeController extends Controller
{

    public function mapGameSelect(){
        $mapModel = new Map();
        $data = $mapModel
            ->select([
                'name',
                'id',
                'id as key'
            ])
            ->where('status',1)
            ->where('type',MapConsts::MAP_TYPE_GAME)
//            ->where('lang_type',LangConsts::getLangCodeByStr(get_lang()))
            ->get();
        return c_return(0,$data);
    }

    public function mapArticleSelect(){
        $mapModel = new Map();
        $data = $mapModel
            ->select([
                'name',
                'id',
                'id as key'
            ])
            ->where('status',1)
            ->where('type',MapConsts::MAP_TYPE_ARTICLE)
//            ->where('lang_type',LangConsts::getLangCodeByStr(get_lang()))
            ->get();
        return c_return(0,$data);
    }


    public function mapGameList(ListRequest $request){
        $mapMode = new Map();
        $pageSize = $request->post('page_size',1);
        $data = $mapMode->select([
            'id',
            'name',
            'remark',
            'lang_type as lang_type_str',
            'id as key',
        ])->where('status',1)
        ->where('type',MapConsts::MAP_TYPE_GAME)
        ->orderBy('id','desc')
        ->paginate($pageSize);
        $data  = get_simple_page($data);
        $game = new Game();
        foreach($data['data'] as $key => $value){
            $data['data'][$key]['number'] = $game->where('game_type_id',$value['id'])->count();
        }

        return c_return(0,$data);
    }

    public function mapArticleList(ListRequest $request){
        $mapMode = new Map();
        $pageSize = $request->post('page_size',1);
        $data = $mapMode->select([
            'id',
            'name',
            'remark',
            'lang_type as lang_type_str',
            'id as key',
        ])->where('status',1)
            ->orderBy('id','desc')
            ->where('type',MapConsts::MAP_TYPE_ARTICLE)
            ->paginate($pageSize);
        $data  = get_simple_page($data);
        $article = new Article();
        foreach($data['data'] as $key => $value){
            $data['data'][$key]['number'] = $article->where('article_type_id',$value['id'])->count();
        }

        return c_return(0,$data);
    }


    public function mapAdd(MapAddRequest $request){
        $data = [
            'name' => $request->post('name'),
            'type' => $request->post('type'),
            // 暂时写死type 为 1
            'lang_type' => 1 ,
            'remark' => $request->post('remark'),
        ];

        $mapModel = new Map();
        $count = $mapModel->where('name',$data['name'])
            ->where('type',$data['type'])
            ->where('lang_type',$data['lang_type'])
            ->where('status',1)
            ->count();
        if($count){
            cp_return(__('message.isExists',[
                'p'=> __('key.map')
            ]));
        }

        $id = $mapModel->insertGetId($data);
        return c_return(0,[
            'id'=> $id
        ]);
    }

    public function mapEdit(){
        $request = request();
        $id = $request->post('id',0);
        $data = [
            'name' => $request->post('name'),
            'type' => $request->post('type',2),
            'remark' => $request->post('remark'),
        ];
        $mapModel = new Map();

        $id = $mapModel->where('id',$id)->update($data);
        return c_return(0,[
            'id'=> $id
        ]);
    }

    public function mapDel(){
        $id = request()->post('id',0);
        $mapModel = new Map();
        try {
            // 校验当前分类是否被使用
            $article_is_exists = (new Article())->where('article_type_id',$id)->first();
            $game_is_exists = (new Game())->where('game_type_id',$id)->first();
            if($article_is_exists || $game_is_exists){
                return ce_return(__('message.map_used'));
            }
            $mapModel->where('id',$id)->where('status',1)->update([
                'status' => 2
            ]);
            return c_return(0,null);
        }catch (\Exception $exception){

            cp_return(__('message.operation_failed'));
        }

    }
}
